////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2021 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////

#include <string>
#include "ohos_utils/napi_handler.h"
#include  "ohos_utils/ohos_log.h"

#include "platform.h"
#include "../realm_dart.h"

static std::string mFilesDir;
static std::string mDeviceName;
static std::string mDeviceVersion;
static std::string mBundleId;

static napi_value native_initRealm(napi_env env, napi_callback_info info) {
    NapiHandler napiHandler(env, info, PARAM_COUNT_4);
    mFilesDir = napiHandler.ParseArgAs<std::string>(INDEX_0);
    mDeviceName = napiHandler.ParseArgAs<std::string>(INDEX_1);
    mDeviceVersion = napiHandler.ParseArgAs<std::string>(INDEX_2);
    mBundleId = napiHandler.ParseArgAs<std::string>(INDEX_3);
    return napiHandler.GetVoidValue();
}

const char *realm_dart_get_files_path()
{
    return mFilesDir.c_str();
}

const char *realm_dart_get_device_name()
{
    return mDeviceName.c_str();
}

const char *realm_dart_get_device_version()
{
    return mDeviceVersion.c_str();
}

const char * realm_dart_get_bundle_id()
{
    return mBundleId.c_str();
}

static napi_value _realm_dart_get_files_path(napi_env env, napi_callback_info info) {
    NapiHandler napiHandler(env, info);
    return napiHandler.GetNapiValue<std::string>(mFilesDir);
}

static napi_value _realm_dart_get_device_name(napi_env env, napi_callback_info info) {
    NapiHandler napiHandler(env, info);
    return napiHandler.GetNapiValue<std::string>(mDeviceName);
}

static napi_value _realm_dart_get_device_version(napi_env env, napi_callback_info info) {
    NapiHandler napiHandler(env, info);
    return napiHandler.GetNapiValue<std::string>(mDeviceVersion);
}

static napi_value _realm_dart_get_bundle_id(napi_env env, napi_callback_info info) {
    NapiHandler napiHandler(env, info);
    return napiHandler.GetNapiValue<std::string>(mBundleId);
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) {
    napi_property_descriptor desc[] = {
        DECLARE_NAPI_FUNCTION("initRealm", native_initRealm),
        DECLARE_NAPI_FUNCTION("getFilesPath", _realm_dart_get_files_path),
        DECLARE_NAPI_FUNCTION("getDeviceName", _realm_dart_get_device_name),
        DECLARE_NAPI_FUNCTION("getDeviceVersion", _realm_dart_get_device_version),
        DECLARE_NAPI_FUNCTION("getBundleId", _realm_dart_get_bundle_id)
    };
    NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
    return exports;
}

EXTERN_C_END
static napi_module demoModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "realm_dart_napi",
    .nm_priv = ((void *)0),
    .reserved = {0},
};
extern "C" __attribute__((constructor)) void RegisterModule(void) {
    LOGE("RegisterModule call");
    napi_module_register(&demoModule);
}